package question_factory;

import models.AbstractQuestion;
import models.TrueFalseQuestion;
import answers.IAnswer;
import answers.TrueFalseAnswer;

public class TrueFalseQuestionFactory implements IQuestionFactory
{

	@Override
	public AbstractQuestion createQuestion(String encoding)
	{
		String [] tokens = encoding.split("\\s");
		try
		{
			double points = Double.parseDouble(tokens[2]);
			String caption = new String();
			
			int answerFlagPos;
			for(answerFlagPos = 3; answerFlagPos < tokens.length; answerFlagPos++)
			{
				if(tokens[answerFlagPos].equals("-a"))
				{
					break;
				}
			}
			
			for(int pos = 3; pos < answerFlagPos; pos++)
			{
				caption += tokens[pos] + " ";
			}
			
			int answerFlag = -1;
			if(answerFlagPos < tokens.length)
			{
				// 0 is false, 1 is true
				answerFlag = Integer.parseInt(tokens[answerFlagPos + 1]);
			}
			
			TrueFalseQuestion question = new TrueFalseQuestion("True-False question", caption, points);
			if(answerFlag > -1)
			{
				IAnswer answer = new TrueFalseAnswer(answerFlag == 1);
				question.setItsIAnswer(answer);
			}
			return question;
		}
		catch(NumberFormatException e)
		{
			System.out.println("Wrong question format");
			return null;
		}
	}

}
